package modules.lobby.view
{
	import flash.display.BitmapData;
	import flash.geom.Matrix;
	import flash.utils.Dictionary;

	import br.com.stimuli.loading.loadingtypes.LoadingItem;

	import modules.GameDispatcher;
	import modules.load.LoadEvent;

	public class JackPotNumber
	{
		public static var dispatcher:GameDispatcher = GameDispatcher.instance;

		private static var _loadingPaths:Array;

		private static var isLoad:Boolean = false;

		public static function get loading():Boolean
		{
			return _loading;
		}

		public static function set loading(value:Boolean):void
		{
			_loading = value;
			if (_loading)
				dispatcher.addEventListener(LoadEvent.LOAD_SINGLE_COMPLETE, onLoadSingleComplete);
			else
				dispatcher.removeEventListener(LoadEvent.LOAD_SINGLE_COMPLETE, onLoadSingleComplete);
		}

		private static function get loadingPaths():Array
		{
			if (_loadingPaths == null)
			{
				_loadingPaths = ["number/0.PNG", //
					"number/1.PNG", //
					"number/2.PNG", //
					"number/3.PNG", //
					"number/4.PNG", //
					"number/5.PNG", //
					"number/6.PNG", //
					"number/7.PNG", //
					"number/8.PNG", //
					"number/9.PNG", //
					"number/comma.PNG", //
					"number/RMB.PNG", //
					];
				for (var i:int = 0; i < _loadingPaths.length; i++)
				{
					_loadingPaths[i] = GlobalData.rootPath + "resources/lobby/" + _loadingPaths[i];
				}
			}
			return _loadingPaths;
		}

		private static var _loading:Boolean = false;

		private static var numberMap:Dictionary = new Dictionary();

		public static function getJackPotNumber(type:String, jackPot:Number):BitmapData
		{
			if (!isLoad)
			{
				if (loading == false)
				{
					loading = true;
					dispatcher.dispatchEvent(new LoadEvent(LoadEvent.LOAD_RESOURCE, {urls: loadingPaths}));
				}
				return null;
			}

			var jackPotStr:String = jackPot.toFixed(0);
			var jackArr:Array = [];
			var index:int = jackPotStr.length;
			while (index > 3)
			{
				jackArr.unshift(jackPotStr.substr(index - 3, 3));
				index = index - 3;
			}
			jackArr.unshift(jackPotStr.substring(0, index));
			var newJackPotStr:String = jackArr.join(",");
			var bmdW:int = 1;
			var bmdH:int = 1;
			bmdW += numberMap[type].width;
			for (var i:int = 0; i < newJackPotStr.length; i++)
			{
				bmdW += numberMap[newJackPotStr.charAt(i)].width;
				bmdH = Math.max(bmdH, numberMap[newJackPotStr.charAt(i)].height);
			}
			var newBmp:BitmapData = new BitmapData(bmdW, bmdH, true, 0x00ffffff);
			var currentX:int = 0;

			newBmp.draw(numberMap[type], new Matrix(1, 0, 0, 1, currentX, 0));
			currentX += numberMap[type].width;
			for (var j:int = 0; j < newJackPotStr.length; j++)
			{
				var drawBmd:BitmapData = numberMap[newJackPotStr.charAt(j)];
				newBmp.draw(drawBmd, new Matrix(1, 0, 0, 1, currentX, 0));
				currentX += drawBmd.width;
			}
			return newBmp;
		}

		private static function onLoadSingleComplete(event:LoadEvent):void
		{
			if (loadingPaths.length == 0)
				return;
			var loadingItem:LoadingItem = event.data;
			var url:String = loadingItem.url.url;
			var index:int = loadingPaths.indexOf(url);
			if (index != -1)
			{
				loadingPaths.splice(index, 1);
				if (loadingPaths.length == 0)
				{
					loading = false;
					isLoad = true;
				}



				var imageName:String = url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf("."));
				switch (imageName)
				{
					case "0":
					case "1":
					case "2":
					case "3":
					case "4":
					case "5":
					case "6":
					case "7":
					case "8":
					case "9":
					case "RMB":
						numberMap[imageName] = loadingItem.content.bitmapData;
						break;
					case "comma":
						numberMap[","] = loadingItem.content.bitmapData;
						break;
				}

			}
		}
	}

}
